#define SystemTick_GLOBALS
#include "cclk.h"
#include "Keypad.H"
#include "SystemTick.h"

//#include "main.h"

/**
 */
__weak void Tick_Handler(void)
{
}
/**
 */
__weak void SystemTickExec(void)
{
	//PBout(12) = !PBin(12);
}
/**
 */
__weak void UserProgram(void)
{
	// PBout(12) = !PBin(12);
	// EC11_proc();
}
/**
 */
__weak void SecProgram(void)
{
	// PBout(12) = !PBin(12);
}

void SystemTick(void) //
{
	if(bSystemTick)
	{
		bSystemTick = 0;
		if(gbKeyInit)
			KeyScanTask();
		else
			KeyScanInit();
		SystemTickExec();
	}
	if(bLongTick)
	{
		bLongTick = 0;
		UserProgram();
	}
	if(bSecTick)
	{
		bSecTick = 0;
		SecProgram();
	}
}

static __INLINE void BB_EXE(void)
{
	switch(bb_n)
	{
		case 0:
			buzzer_ctr(0);
			break;
		case(-2):
			buzzer_ctr(!buzzer_get());
			break;
		case(-1):
			buzzer_ctr(1);
			if(bb_t)
			{
				if(--bb_t == 0)
				{
					bb_n = 0;
					buzzer_ctr(0);
				}
			}
			break;
		default:
			if(!buzzer_get())
				bb_n--;
			buzzer_ctr(!buzzer_get());
			break;
	}
}
extern void mb_timeout_tick(void);
extern void idle_timeout_tick(void);
void HAL_IncTick(void)
{
	idle_timeout_tick();
#ifdef UART_MB_M
	mb_timeout_tick();
#endif
	uwTick += uwTickFreq;
	static u32 sec_tick = TICKS_PER_SEC * 2;
	if(--sec_tick == 0)
	{
		sec_tick = TICKS_PER_SEC * 2;
		// PBout(14) = !PBin(14);//SPI2
		bSecTick = 1;
		CCLK_INC();
	}
	else
	{
		if(sec_tick & 1)
		{
			Tick_Handler();
			bSystemTick = 1;
			// PBout(12) = !PBin(12);//SPI2
		}
		else if(sec_tick % (2 * TICKS_PER_SEC / 10) == TICKS_PER_SEC / 10)
		{
			bLongTick = 1;
			// PBout(13) = !PBin(13);//SPI2
			BB_EXE();
			// SYS_Timeout_porc();
		}
	}
}

